package com.kobe.game_60;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class _54 {

    private class Hand {
        int[] pokers = new int[5];
        int four = 0;
        int three = 0;
        int[] pair = new int[2];

        boolean isFlush = true;

        private int getNumValue(char chr) {
            switch (chr) {
            case 'T':
                return 10;
            case 'J':
                return 11;
            case 'Q':
                return 12;
            case 'K':
                return 13;
            case 'A':
                return 14;
            default:
                return Character.getNumericValue(chr);
            }
        }

        public Hand(String pokerString) {
            this(pokerString.split(" "));
        }

        public Hand(String[] pokersArray) {
            char type = 'N';
            for (int i = 0; i < pokersArray.length; i++) {
                pokers[i] = getNumValue(pokersArray[i].charAt(0));
                if (type == 'N') {
                    type = pokersArray[i].charAt(1);
                } else if (type != pokersArray[i].charAt(1)) {
                    isFlush = false;
                }
            }
            Arrays.sort(pokers);
        }

        private boolean isStraight(int start) {
            for (int temp : pokers)
                if (temp != start++)
                    return false;
            return true;
        }

        private int getIndividalPokersExponent() {
            int result = 0;
            int mod = 100;
            int stepper = 1;
            for (int temp : pokers) {
                if (temp != four && temp != three && temp != pair[0]
                        && temp != pair[1]) {
                    result += stepper * temp;
                    stepper *= mod;
                }
            }
            return result;
        }

        private double caculateStrongExponent() {
            double result = 0;
            if (this.isRoyalFlush()) {
                result = 10;
            } else if (this.isStraightFlush()) {
                result = 9 + pokers[0] / 100d;
            } else if (this.isFourOfAKind()) {
                result = 8 + four / 100d;
            } else if (this.isFullHouse()) {
                result = 7 + three / 100d + pair[0] / 10000d;
            } else if (this.isFlush()) {
                result = 6 + pokers[4] / 100d + pokers[3] / 10000d + pokers[2]
                        / 1000000d + pokers[1] / 100d + pokers[0] / 100000000d;
            } else if (this.isStraight()) {
                result = 5 + pokers[0] / 100d;
            } else if (this.isThreeOfAKind()) {
                result = 4 + three / 100d + getIndividalPokersExponent()
                        / 1000d;
            } else if (this.isTwoPairs()) {
                int[] pairClone = pair.clone();
                Arrays.sort(pairClone);
                result = 3 + pairClone[1] / 100d + pairClone[0] / 10000d
                        + getIndividalPokersExponent() / 1000000d;
            } else if (this.isOnePair()) {
                result = 2 + pair[0] / 100d + getIndividalPokersExponent()
                        / 10000000000d;
            } else {
                result = 1 + getIndividalPokersExponent() / 10000000000d;
            }

            return result;
        }

        public boolean stronger(Hand hand) {
            if (this.caculateStrongExponent() > hand.caculateStrongExponent()) {
                return true;
            }
            return false;
        }

        private boolean isOnePair() {
            return (pair[0] != 0) ? true : false;
        }

        private boolean isTwoPairs() {
            return (pair[0] != 0 && pair[1] != 0) ? true : false;
        }

        private boolean isThreeOfAKind() {
            return (three != 0) ? true : false;
        }

        private boolean isStraight() {
            return isStraight(pokers[0]);
        }

        private boolean isFlush() {
            return isFlush;
        }

        private boolean isFullHouse() {
            int found = 0;
            int tempPoker = 0;
            int pairIndex = 0;
            for (int temp : pokers) {
                if (tempPoker != temp) {
                    tempPoker = temp;
                    found = 1;
                } else {
                    found++;
                }
                if (found == 2) {
                    pair[pairIndex++] = temp;
                }
                if (found == 3) {
                    three = temp;
                    if (pair[--pairIndex] == three) {
                        pair[pairIndex] = 0;
                    }
                }
            }
            return (three != 0 && pair[0] != 0) ? true : false;
        }

        private boolean isFourOfAKind() {
            int found = 0;
            int tempPoker = 0;
            for (int temp : pokers) {
                if (tempPoker != temp) {
                    tempPoker = temp;
                    found = 1;
                } else {
                    found++;
                }
                if (found == 4) {
                    four = temp;
                    return true;
                }
            }
            return false;
        }

        private boolean isStraightFlush() {
            if (isStraight() && isFlush)
                return true;
            return false;
        }

        private boolean isRoyalFlush() {
            if (isStraight(10) && isFlush)
                return true;
            return false;
        }
        
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(
                "source/poker.txt"));
        String line = null;
        String[] stringArray = null;
        String[] p1Pokers = new String[5];
        String[] p2Pokers = new String[5];
        int p1Win = 0;
        while ((line = reader.readLine()) != null) {
            stringArray = line.split(" ");
            System.arraycopy(stringArray, 0, p1Pokers, 0, 5);
            System.arraycopy(stringArray, 5, p2Pokers, 0, 5);
            Hand p1 = new _54().new Hand(p1Pokers);
            Hand p2 = new _54().new Hand(p2Pokers);
            if (p1.stronger(p2)){
                p1Win++;
            }
        }
        System.out.println("P1 total wins: "+p1Win);
    }
}
